package app;

import java.util.*;

import org.w3c.dom.Element;

import utils.FilePack;
import utils.MD5;
import utils.XMLReader;

import com.StarMicronics.StarIOSDK.R;

import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class LoginView implements RequestListener, NotificationListener  {

	private WaittingAlertView waittingView;
	private EditText user_name;
	private EditText truck_id;
	private EditText truck_id_again;
	private EditText password;
	private Button login_button;
	private Button server_button;
	public View loginView;
	
	public LoginView() {
        View view = AppDelegate.sharedInstance().activity.getLayoutInflater().inflate(R.layout.login, null);
		// TODO Auto-generated constructor stub
		loginView = view;
		user_name = (EditText) view.findViewById(R.id.edit_user_name);
		truck_id = (EditText) view.findViewById(R.id.edit_truck_id);
		truck_id_again = (EditText) view.findViewById(R.id.edit_truck_id_again);
		password = (EditText) view.findViewById(R.id.edit_password);


		login_button = (Button) view.findViewById(R.id.button_login);
		login_button.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		login();
        	}
		});

		server_button = (Button)view.findViewById(R.id.button_server);
		server_button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				server();
			}
		});
		
		view.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
        	}
		});
		
		updateLanguage();
	}
	
	private void login() {
		if (user_name.getText().length() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_NEED_USER_NAME));
			alert.show();
		} else if (truck_id.getText().length() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_NEED_TRUCK_ID));
			alert.show();
		} else if (truck_id_again.getText().length() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle("Please input confirm truck number");
			alert.show();
		} else if (password.getText().length() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_NEED_PASSWORD));
			alert.show();
		} else if (!truck_id.getText().toString().equals(truck_id_again.getText().toString())) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle("Truck number is not the same");
			alert.show();
		} else {
			String name = user_name.getText().toString();
			String number = truck_id.getText().toString();
			String pass = password.getText().toString();

			byte[] bytes_pass = MD5.getMD5Bytes(pass);
			pass = MD5.getMD5(pass);
			
			Dictionary<String, Object> body = new Hashtable<String, Object>();
			body.put("uName", name);
			body.put("tNumber", number);
			body.put("pWord", bytes_pass);
			
			Dictionary<String, Object> params = new Hashtable<String, Object>();
			params.put("truckID", number);
			AppDelegate.sharedInstance().postHttpRequest("Login", body, params, this, false);
			
			
			waittingView = WaittingAlertView.createWaittingAlertView();
			waittingView.setLoadingText(FilePack.getStringByID(STRING_ID.STR_PLEASE_WAIT));
			waittingView.show();
		}
	}
	
	private void server() {
		ServerView server = new ServerView();
		server.addToWindow();
		server.bind();
	}

	public void requestFinished(RequestItem request, Dictionary<String, Object> params,
			Element data, boolean successfull) {
		// TODO Auto-generated method stub
		if (successfull) {
			if (XMLReader.getIntForKey(data, "ret", 0) != 1) {
				successfull = false;
			}
		}
		
		if (successfull) {
			AppDelegate.sharedInstance().token = XMLReader.getStringForKey(data, "token", "");
			AppDelegate.sharedInstance().uid = XMLReader.getStringForKey(data, "uid", "");
			AppDelegate.sharedInstance().truckID = (String) params.get("truckID");
			waittingView.cancel();
			AppDelegate.sharedInstance().login();
			remove();
			waittingView = null;
			
			AppDelegate.sharedInstance().postNotification(LoginSuccessfullListener.class, null, null);
		} else {
			waittingView.setTitle(XMLReader.getStringForKey(data, "error", FilePack.getStringByID(STRING_ID.STR_ERROR_OCCUR)));
			//waittingView.cancel();
			//waittingView = null;
		}
	}

	private void updateLanguage() {
		user_name.setHint(FilePack.getStringByID(STRING_ID.STR_USER_NAME));
		truck_id.setHint(FilePack.getStringByID(STRING_ID.STR_TRUCK_ID));
		truck_id_again.setHint("CONFIRM TRUCK #");
		password.setHint(FilePack.getStringByID(STRING_ID.STR_PASSWORD));
		login_button.setText(FilePack.getStringByID(STRING_ID.STR_SIGN_IN));
	}

	public void addToWindow() {
		AppDelegate.sharedInstance().layout.addView(loginView);
		AppDelegate.sharedInstance().addNotificationListener(MultiLanguageChangeNotificationListener.class, this);
		updateLanguage();
	}
	
	public void remove() {
		AppDelegate.sharedInstance().layout.removeView(loginView);
		AppDelegate.sharedInstance().removeNotificationListener(MultiLanguageChangeNotificationListener.class, this);
	}

	public void notification(Class<?> clss, Object object, Object userInfo) {
		if (clss == MultiLanguageChangeNotificationListener.class) {
			updateLanguage();
		}
	}
}
